﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FetionWebMessenger.AddWebService;
namespace FetionWebMessenger
{
    public partial class FetionAction : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpRequest request = this.Page.Request;
            String action = request.Params["action"];
            HttpResponse response = this.Page.Response;
            response.ContentType = "text/xml";
            response.AppendHeader("Cache-Control", "no-cache");
            String result = "<?xml version='1.0' encoding='UTF-8'?><response>";
            if (action!="login")
            {
                if (SessionObj == null)
                {
                    result += "<msg>nolgoin</msg>";
                    result += "</response>";
                    response.Redirect("WebRoot/login.html", true);
                    response.Write(result);
                    return;
                }
            }
            string uri, msg;
            switch (action)//动作派发
            {
                case "login"://登陆动作调用
                    string mobile = request.Params["mobile"];
                    string password = request.Params["password"];
                    result += LoginAction(mobile, password);
                    break;
                case "logout"://登出操作调用
                    if (LogoutAction())
                    {
                        result += "<msg>success</msg>";
                    }
                    else
                    {
                        result += "<msg>failed</msg>";
                    }
                   response.Redirect("WebRoot/login.html", true);
                    break;
                case "peekstate":
                    result += "<msg>" + SessionObj.statePool + "</msg>";
                    break;
                case "peekmsg"://获取消息的调用
                    result += "<msg>peekmsg</msg>";
                    result += SessionObj.PeekMessage();
                    break;
                case "sendmsg"://发送消息的调用_到fetion
                    uri = request.Params["uri"];
                    msg = request.Params["msg"];
                    if (SessionObj.SendMsgTo(uri,msg))
                    {
                        result +="<msg>success</msg>" ;
                    }
                    else
                    {
                        result += "<msg>failed</msg>";
                    }
                    break;
                case "sendsms"://发送消息的调用_到fetion
                    uri = request.Params["uri"];
                    msg = request.Params["msg"];
                    if (SessionObj.SendSMSTo(uri, msg))
                    {
                        result += "<msg>success</msg>";
                    }
                    else
                    {
                        result += "<msg>failed</msg>";
                    }
                    break;
                case "peekfriendslist"://获取好友列表
                    result += "<msg>friendslist</msg>";
                    result += SessionObj.GetFriendsList();
                    break;
                case "addbuddy"://添加好友
                    string mobileNo = request.Params["mobile"];
                    string desc = request.Params["desc"];
                    result += "<msg>addbuddy</msg>";
                    SessionObj.AddBuddy(mobileNo,desc);
                    break;
                case "acceptebuddy"://接受某人为好友
                    break;
                case "peeksysmsg"://试图获取系统消息
                    result += "<msg>peeksysmsg</msg>";
                    result += SessionObj.PeekSysMessage();
                    break;
                case "peekme":
                    result += "<msg>peekme</msg>";
                    result += SessionObj.GetMyInfo();
                    break;
                case "addWeather"://调用添加天气订阅
                    string city = request.Params["city"];
                    FetionWebMessenger.AddWebService.SubscribeManager.AddWeatherSubscriber(SessionObj.Uri, city);
                    break;
                case "sendToself":
                    {
                        String content = request.Params["content"];
                        SessionObj.SendToSelf(content);
                        break;
                    }
                default://未定义的动作请求
                    //LoginAction("15900582223", "yaojunguang");
                    result += "<msg>undefinedAction</msg>";
                    break;
            }
            result += "</response>";
            response.Write(result);
        }
        protected String LoginAction(string mobile, string password)
        {
            FetionCore fetionCore = new FetionCore();
            fetionCore.LoginFetion(mobile, password);
            Session["FetionCore"] = fetionCore;
            return "<msg>success</msg>";
        }
        protected Boolean LogoutAction()
        {
            SessionObj.LogoutFetion();
            Session.RemoveAll();
            return true;
        }
        protected FetionCore SessionObj
        {
            get
            {
                return (FetionCore)Session["FetionCore"];
            }
        }
    }
}
